﻿//
// GNS_ApplyTick.cpp
// MobileManageSys
//FuncTion 申请兑奖
// Created by mr.l on 16/1/14.
//
//
#include "GnsClientFrame.h"
#include "GNS_ApplyTick.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_Common.h"
#include "GnsOften.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

////extern ISocketMgr * _GameSock;
#define winsize Director::getInstance()->getVisibleSize()

GNS_ApplyTick::GNS_ApplyTick()
:_TopID(nullptr)
, _TopUplines(nullptr)
{
    pGNS_ApplyTick=0;
    TopID=nullptr;
//    NoteTheName=nullptr;
    TopUplines=nullptr;
    RLines=nullptr;
    NoteManage=nullptr;
    pPopWnd=nullptr;
}

GNS_ApplyTick::~GNS_ApplyTick()
{
    
}

GNS_ApplyTick* GNS_ApplyTick::createLayer()
{
    auto pGNS_ApplyTick = GNS_ApplyTick::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_ApplyTick);
    return pGNS_ApplyTick;
}

 /*Scene * GNS_ApplyTick::createScene()
{
  
    auto scene = Scene::create();
    auto layer = GNS_ApplyTick::create();
    scene->addChild(layer);
    return scene;
  
}  */

bool GNS_ApplyTick::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    return true;
}
void GNS_ApplyTick::SetNodeName(std::string CsbName)
{
    pGNS_ApplyTick = CSLoader::createNode(CsbName);
    pGNS_ApplyTick->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_ApplyTick);
    addChild(pGNS_ApplyTick,11);
    
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_ApplyTick);
    auto pBackBtn = static_cast<Button*>(pGNS_ApplyTick->getChildByName("ApplyTickBackBtn"));
    pBackBtn->setTag(10);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_ApplyTick::BtnCallBack, this));
    
    auto pSendDate = static_cast<Button*>(pGNS_ApplyTick->getChildByName("TickSender"));
    pSendDate->setTag(1);
    pSendDate->addTouchEventListener(CC_CALLBACK_2(GNS_ApplyTick::BtnCallBack, this));
    
    
    TopID = static_cast<TextField*>(pGNS_ApplyTick->getChildByName("TickText"));
	int uplineCode = GNS_PlayerMgr::getInstance()->pGetUserInfoResult._uplineCode;
	TopID->setString(StringUtils::format("%d", uplineCode));
    //TopID->setInsertText(true);
    //TopID->addEventListener(CC_CALLBACK_2(GNS_ApplyTick::textFieldEvent, this));
	_TopID = GNS_Handler::createTMEditBoxWithExistFeild(TopID, pGNS_ApplyTick, 14, EditBox::InputMode::NUMERIC);
    
    TopUplines = static_cast<TextField*>(pGNS_ApplyTick->getChildByName("TickText1"));
    //TopUplines->addEventListener(CC_CALLBACK_2(GNS_ApplyTick::textFieldEvent, this));
	_TopUplines = GNS_Handler::createTMEditBoxWithExistFeild(TopUplines, pGNS_ApplyTick, 10, EditBox::InputMode::NUMERIC);
	_TopUplines->addEditingEndCallback(CC_CALLBACK_0(GNS_ApplyTick::textFieldEditEnd, this));
	
    RLines = static_cast<Text*>(pGNS_ApplyTick->getChildByName("TickText3"));

    NoteManage = static_cast<TextField*>(pGNS_ApplyTick->getChildByName("TickText2"));
    
    auto TopSentDate = static_cast<Button*>(pGNS_ApplyTick->getChildByName("TickSender"));
    TopSentDate->setTag(1);
    TopSentDate->addTouchEventListener(CC_CALLBACK_2(GNS_ApplyTick::BtnCallBack, this));
}

void GNS_ApplyTick::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 10:
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.clear();
                    this->removeAllChildrenWithCleanup(true);
                    this->getParent()->removeChild(this);
                }
                //auto SystemNode=GNS_PlayerMgr::getInstance()->_NodeHallVec.at(0);
                //SystemNode->setVisible(true);

				GNS_NodeMgr::getInstance()->setMenuVisible("SystemMgr", true);
                break;
            }
            case 1:
            {
                SenDetection();
                break;
            }
            default:
                break;
        }
    }
}
void GNS_ApplyTick::SenDetection()
{
	string topid = _TopID->getText();
	string gold = _TopUplines->getText();
	string remark = NoteManage->getString();
    if (!CCGnsOften::checkMgrID(topid)){
		OnUIAction("兑奖对象错误！", true);
    }
	else if (gold == "0" || !CCGnsOften::isNumber(gold) )
	{
		OnUIAction("兑奖金额错误！", true);
	}
	else if (!CCGnsOften::checkBeiZhu(remark))
	{
		OnUIAction("备注过长！", true);
	}
	else
	{
        CMD_CSG_AppToRechageOrExchange pAppToReOrEx;
        pAppToReOrEx._sendAgentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
        pAppToReOrEx._revAgentCode = atoi(_TopID->getText());
        pAppToReOrEx._rechangeGold = atoi(_TopUplines->getText());
        pAppToReOrEx._operType =2;
		string remarkTmp = SystemMgr::getInstance()->utf8ToGb(NoteManage->getString().c_str());
        memcpy(pAppToReOrEx._remark,remarkTmp.c_str(), remarkTmp.length()+1);
        ////_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CSG_REQUEST_EXCHANGE_RMB_TO_GOLD, &pAppToReOrEx, sizeof(pAppToReOrEx));
        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CSG_REQUEST_EXCHANGE_RMB_TO_GOLD, (char*)&pAppToReOrEx, sizeof(pAppToReOrEx));
    }
}

void GNS_ApplyTick::textFieldEditEnd()
{
	int id = GNS_PlayerMgr::getInstance()->getMyUseridx();
	
	for (auto it : (*GNS_PlayerMgr::getInstance()->getAgRation())) 
	{
		int testid = it._lowlineCode;
		if (it._lowlineCode == id) 
		{
			int TopUplin = atoi(_TopUplines->getText());
			int RLinesNum = TopUplin / it._lowlineRechargeRatio;
			RLines->setText(StringUtils::format("%d", RLinesNum).c_str());
		}
	}
	
}


void GNS_ApplyTick::OnUIAction(std::string Text, bool isUTF)
{
	pPopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
	pPopWnd->setClickWndOutsideClose(true);
	pPopWnd->SetTitleWithUTF("提示!");
	if (isUTF)
		pPopWnd->SetContentTextWithUTF(Text, Color3B::BLACK);
	else
		pPopWnd->SetContentTextWithGB(Text, Color3B::BLACK);
}

//void GNS_ApplyTick::textFieldEvent(cocos2d::Ref *pSender, ui::TextField::EventType type)
//{
//    auto pTextField = (TextField*)(pSender);
//    
//    switch (type) {
//        case TextField::EventType::ATTACH_WITH_IME:
//        {
//            log("1");
//            break;
//        }
//        case TextField::EventType::DETACH_WITH_IME:
//        {
//            //std::string InputStr=TopID->getString();
//			int id = GNS_PlayerMgr::getInstance()->getMyUseridx();
//            if (pTextField==TopUplines){
//                for (auto it:(*GNS_PlayerMgr::getInstance()->getAgRation())) {
//					int testid = it._lowlineCode;
//                    if (it._lowlineCode==id) {
//                        int TopUplin = atoi(TopUplines->getString().c_str());
//                        int RLinesNum = TopUplin/it._lowlineRechargeRatio;
//                        RLines->setString(StringUtils::format("%d",RLinesNum));
//                    }
//                }
//            }
//            break;
//        }
//        case TextField::EventType::INSERT_TEXT:
//        {
//            log("3");
//            break;
//        }
//        case TextField::EventType::DELETE_BACKWARD:
//        {
//            log("4");
//            break;
//        }
//        default:
//            break;
//    }
//}